"""
AI分析结果相关的Pydantic模式
"""
from pydantic import BaseModel, Field
from typing import Optional, List, Any, Dict
from datetime import datetime
from enum import Enum


class AIResultType(str, Enum):
    """AI结果类型枚举"""
    BEHAVIOR_ANALYSIS = "behavior_analysis"
    CROWD_DENSITY = "crowd_density"
    WASTE_DETECTION = "waste_detection"
    CLEANLINESS_ASSESSMENT = "cleanliness_assessment"
    TEACHING_QUALITY = "teaching_quality"
    CLASSROOM_QUALITY = "classroom_quality"
    CONFLICT_DETECTION = "conflict_detection"
    POSE_DETECTION = "pose_detection"
    TRAJECTORY_ANALYSIS = "trajectory_analysis"
    LIGHTING_DETECTION = "lighting_detection"
    ENERGY_ANALYSIS = "energy_analysis"


class AIResultBase(BaseModel):
    """AI分析结果基础模式"""
    camera_id: int = Field(..., description="摄像头ID")
    algorithm_name: str = Field(..., max_length=100, description="算法名称")
    result_type: AIResultType = Field(..., description="结果类型")
    result_data: Dict[str, Any] = Field(..., description="分析结果数据")
    confidence: Optional[float] = Field(None, ge=0, le=1, description="置信度")
    bounding_boxes: Optional[List[Dict[str, Any]]] = Field(None, description="边界框数据")
    timestamp: datetime = Field(..., description="分析时间戳")
    extra_data: Optional[Dict[str, Any]] = Field(None, description="额外数据")
    
    # 实时处理相关字段
    frame_timestamp: Optional[datetime] = Field(None, description="视频帧时间戳")
    frame_id: Optional[str] = Field(None, max_length=100, description="帧ID")
    stream_info: Optional[Dict[str, Any]] = Field(None, description="流信息")
    is_real_time: bool = Field(default=False, description="是否实时处理")
    batch_id: Optional[str] = Field(None, max_length=100, description="批处理ID")
    worker_id: Optional[str] = Field(None, max_length=50, description="处理工作器ID")
    gpu_id: Optional[int] = Field(None, description="使用的GPU ID")
    frame_size: Optional[Dict[str, int]] = Field(None, description="帧尺寸信息")
    alert_triggered: bool = Field(default=False, description="是否触发告警")


class AIResultCreate(AIResultBase):
    """创建AI分析结果模式"""
    task_id: Optional[int] = Field(None, description="任务ID")


class AIResultUpdate(BaseModel):
    """更新AI分析结果模式"""
    result_data: Optional[Dict[str, Any]] = None
    confidence: Optional[float] = Field(None, ge=0, le=1)
    bounding_boxes: Optional[List[Dict[str, Any]]] = None
    extra_data: Optional[Dict[str, Any]] = None
    alert_triggered: Optional[bool] = None


class AIResultResponse(AIResultBase):
    """AI分析结果响应模式"""
    id: int
    task_id: Optional[int] = None
    processing_latency: Optional[float] = Field(None, description="处理延迟(ms)")
    processed_at: Optional[datetime] = Field(None, description="处理完成时间")
    created_at: datetime
    updated_at: datetime

    model_config = {"from_attributes": True}


class AIResultListResponse(BaseModel):
    """AI分析结果列表响应模式"""
    items: List[AIResultResponse]
    total: int
    page: int
    size: int
    pages: int


class AIResultFilters(BaseModel):
    """AI分析结果筛选条件"""
    camera_id: Optional[int] = None
    algorithm_name: Optional[str] = None
    result_type: Optional[AIResultType] = None
    is_real_time: Optional[bool] = None
    alert_triggered: Optional[bool] = None
    start_time: Optional[datetime] = None
    end_time: Optional[datetime] = None
    page: int = Field(default=1, ge=1)
    size: int = Field(default=10, ge=1, le=100)


class RealTimeAIResult(BaseModel):
    """实时AI分析结果模式"""
    camera_id: int
    algorithm_name: str
    result_type: AIResultType
    result_data: Dict[str, Any]
    confidence: Optional[float] = None
    bounding_boxes: Optional[List[Dict[str, Any]]] = None
    frame_timestamp: datetime
    frame_id: str
    processing_latency: float
    worker_id: str
    gpu_id: Optional[int] = None
    alert_triggered: bool = False


class AIResultStatistics(BaseModel):
    """AI分析结果统计模式"""
    camera_id: int
    algorithm_name: str
    total_results: int = 0
    avg_confidence: Optional[float] = None
    avg_processing_latency: Optional[float] = None
    alert_count: int = 0
    last_result_time: Optional[datetime] = None
    success_rate: float = 0.0
    error_count: int = 0


class BatchAIResultCreate(BaseModel):
    """批量创建AI分析结果模式"""
    results: List[AIResultCreate] = Field(..., min_length=1, max_length=100)
    batch_id: str = Field(..., description="批处理ID")
    worker_id: str = Field(..., description="处理工作器ID")